//
//  MusicController.m
//  GBKHOME
//
//  Created by XMYY-19 on 2017/9/5.
//
//

#import "MusicController.h"
#import "Global1.h"
#import "LoginController1.h"

@interface MusicController ()
@property (retain, nonatomic) IBOutlet UISlider *ProgressView;
- (IBAction)nextBtnClick:(UIButton *)sender;
- (IBAction)stopBtnClick:(UIButton *)sender;
- (IBAction)preBtnClick:(UIButton *)sender;
@property (retain, nonatomic) IBOutlet UILabel *musicName;
@property (retain, nonatomic) IBOutlet UIImageView *picImageView;
@property (retain, nonatomic) IBOutlet UIButton *leftBtn;
@property (retain, nonatomic) IBOutlet UIButton *middleBtn;
@property (retain, nonatomic) IBOutlet UIButton *rightBtn;
@property (retain, nonatomic) IBOutlet UIButton *subtrVoiceBtn;
@property (retain, nonatomic) IBOutlet UIButton *addVoiceBtn;

@property(nonatomic,assign)BOOL isClosed;
@end
@implementation MusicController



-(id)initWithEquipment:(NewEquipment *)equ
{
    self = [super init];
    if (self)
    {
        self.isClosed = NO;
        self.equipment = equ;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigationTitleView:_equipment.name];
    self.musicName.hidden = YES;

    CGRect curRect = self.picImageView.frame;
    [self.picImageView setFrame:(CGRect){(SCREEN_WIDTH-curRect.size.width)/2,64,curRect.size.width,curRect.size.height}];
    CGFloat imageViewBottom = self.picImageView.frame.origin.y + self.picImageView.frame.size.height;
    CGFloat buttonSize = 40;
    CGFloat playLeft = (SCREEN_WIDTH - 19*4 - 5*40)/2;
    
    [self.subtrVoiceBtn setFrame:(CGRect){playLeft+25,imageViewBottom+43,20,20}];
    [self.subtrVoiceBtn setImage:[UIImage imageNamed:@"vol_minus"] forState:UIControlStateNormal];
    [self.leftBtn setFrame:(CGRect){playLeft+buttonSize+19,imageViewBottom+30,buttonSize,buttonSize}];
    [self.middleBtn setFrame:(CGRect){playLeft+buttonSize*2+19*2,imageViewBottom+30,buttonSize,buttonSize}];
    [self.rightBtn setFrame:(CGRect){playLeft+buttonSize*3+19*3,imageViewBottom+30,buttonSize,buttonSize}];
    [self.addVoiceBtn setFrame:(CGRect){playLeft+buttonSize*4+19*4,imageViewBottom+40,20,20}];
    
    self.subtrVoiceBtn.backgroundColor = [UIColor clearColor];
    self.addVoiceBtn.backgroundColor = [UIColor clearColor];
    
    [self setCtrlButtons];
    [self setFolderButtons];
    
    UIImage *image =[UIImage imageNamed:@"mainbg"];
    self.view.layer.contents = (id)image.CGImage;
}

- (void)dealloc {
    [_picImageView release];
    [_musicName release];
    [_ProgressView release];
    [_leftBtn release];
    [_middleBtn release];
    [_rightBtn release];
    [_subtrVoiceBtn release];
    [_addVoiceBtn release];
    [super dealloc];
}
#pragma mark--下一首歌曲
- (IBAction)nextBtnClick:(UIButton *)sender {
    
       [self ctrDVD:@"NEXT"];
}
#pragma mark--停止歌曲
- (IBAction)stopBtnClick:(UIButton *)sender {
       [self ctrDVD:@"STOP"];
}
#pragma mark--上一首歌曲
- (IBAction)preBtnClick:(UIButton *)sender {
    
    [self ctrDVD:@"PERV"];
}

- (IBAction)subtraVoiceClick:(id)sender {
    [self ctrDVD:@"F1"];
}

- (IBAction)addVoiceClick:(id)sender {
    [self ctrDVD:@"F2"];
}

- (void)setCtrlButtons
{
    NSInteger cols = 5;
    CGFloat w = 50;
    CGFloat h = 40;
    CGFloat marign = (SCREEN_WIDTH - w*5)/6;

    NSInteger col = 0;
    NSArray *backeImageArray = [NSArray arrayWithObjects:@"USB",@"FM",@"SD",@"AUX",@"停止",nil];
    for (int i = 0; i <  backeImageArray.count; i ++) {
        col = i % cols;
        UIButton *subjectsBtn = [[UIButton alloc] initWithFrame:CGRectMake(marign*(i+1)+w*i, self.subtrVoiceBtn.bottom+30, w, h)];
        subjectsBtn.tag = i;
        subjectsBtn.layer.cornerRadius = 10;
        subjectsBtn.backgroundColor =[UIColor colorWithRGBColorByString:@"#17174c"];
        [subjectsBtn setTitle:backeImageArray[i] forState:UIControlStateNormal];
        subjectsBtn.titleLabel.textColor = [UIColor whiteColor];
        [subjectsBtn addTarget:self action:@selector(subjectsButton3:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:subjectsBtn];
    }
}

- (void)subjectsButton3:(UIButton *)subButton
{
    if (subButton.tag == 0) {
        [self ctrDVD:@"USB"];
    } else if (subButton.tag == 1) {
        [self ctrDVD:@"FM"];
    } else if (subButton.tag == 2) {
        [self ctrDVD:@"SD"];
    } else if (subButton.tag == 3) {
        [self ctrDVD:@"AUX"];
    } else if (subButton.tag == 4) {
        [self ctrDVD:@"STOP"];
    }
}

- (void)setFolderButtons
{
    NSInteger cols = 4;
    CGFloat w = 68;
    CGFloat h = 40;
    CGFloat marign = (SCREEN_WIDTH - w*4)/5;
    
    NSInteger col = 0;
    NSArray *backeImageArray = [NSArray arrayWithObjects:@"上目录",@"下目录",@"静音",@"返回",nil];
    for (int i = 0; i <  backeImageArray.count; i ++) {
        col = i % cols;
        UIButton *subjectsBtn = [[UIButton alloc] initWithFrame:CGRectMake(marign*(i+1)+w*i, self.subtrVoiceBtn.bottom+100, w, h)];
        subjectsBtn.tag = i;
        [subjectsBtn setTitle:backeImageArray[i] forState:UIControlStateNormal];
        subjectsBtn.titleLabel.textColor = [UIColor whiteColor];
        subjectsBtn.layer.cornerRadius = 10;
        subjectsBtn.backgroundColor =[UIColor colorWithRGBColorByString:@"#17174c"];

        [subjectsBtn addTarget:self action:@selector(subjectsButton4:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:subjectsBtn];
    }
}

- (void)subjectsButton4:(UIButton *)subButton
{
    if (subButton.tag == 0) {
        [self ctrDVD:@"PD"];
    } else if (subButton.tag == 1) {
        [self ctrDVD:@"ND"];
    } else if (subButton.tag == 2) {
        [self ctrDVD:@"MUTE"];
    } else if (subButton.tag == 3) {
        [self ctrDVD:@"BACK"];
    }
}

#pragma mark - 控制DVD
- (void)ctrDVD:(NSString *)ORDER
{
    NSString *order = ORDER;
    NSString *orderName = NSLocalizedString(order, nil);
    NSString *operat = NSLocalizedString(@"operat:", nil);
    NSString *startTip = [operat stringByAppendingString:orderName];
    [iToast showTip:startTip gravity:iToastGravityBottom];
    
    [WebServices setShowConnectting:NO];
    
    self.ctrOrder = order;
//    NSString *switchId = self.equipment.switchId;
    
    NSString *switchId = @"14";
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    
    //操作失败！原因：
    NSString *errorTip = [OperationTip operatSuccess:NO type:ErrorTypeNetworkFail];
    [WebServices requestStartOrder:@"ctlBackgroundMusic" body:body target:self action:@selector(ctrDVDEnd:) errorTip:errorTip];
}

- (void)ctrDVDEnd:(NSDictionary *)dic
{
    NSLog(@"%@", dic);
    NSNumber *result = [dic objectForKey:@"result"];
    if (result.integerValue == 0) {
        //操作成功
        NSString *tip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
    else
    {
        //操作失败！原因：
        NSString *tip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
}
@end
